Home
Ray
Articles
This Old House
Family History
Validate Forms
Text Box
Please enter your last name.
SAMPLE CODE
Please enter your last name.
function checkForLastName() { var targetedField = document.getElementById("lastNameField") if (targetedField.value.length == 0) { alert("Please enter your last name"); targetedField.focus(); targetedField.style.background = "yellow"; return false; } targetedField.style.background = "white"; }
Drop Down
SELECT a STATE
Alabama
Alaska
Arizona
Arkansas
SAMPLE CODE
SELECT a STATE
Alabama
Alaska
Arizona
Arkansas
function checkForSelection() { var target = document.getElementById("states"); if (target.selectedIndex == 0) { alert("Please select a state."); return false; } }
Radio Buttons
Cat
Bat
Hat
SAMPLE CODE
Cat
Bat
Hat
function validateRadios() { var radios = document.getElementsByName("r1"); for (var i = 0; i < radios.length; i++) { if (radios[i].checked) { return true; } } alert("Please check one."); return false; }